from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import render, HttpResponse, redirect
from django.contrib import auth


class AuthMid(MiddlewareMixin):
  def process_request(self, request):
    # print("md1  process_request 方法。", id(request)) #在视图之前执行
    # print('url=', request.path_info)
    if request.path_info=='/login/' or request.path_info=='/favicon.ico':
      return None
    status = request.session.get('is_login') # 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的 cookie
    if not status:
        return redirect('/login')

  def process_response(self,request, response): #基于请求响应
    # print("md1  process_response 方法！", id(request)) #在视图之后
    return response

  def process_view(self,request, view_func, view_args, view_kwargs):
    pass
    # print("md1  process_view 方法！") #在视图之前执行 顺序执行
    #return view_func(request)

  def process_exception(self, request, exception):#引发错误 才会触发这个方法
    pass
    # print("md1  process_exception 方法！")
    # return HttpResponse(exception) #返回错误信息